package com.simon.spark.sql

import org.apache.spark.sql.{Row, SQLContext}
import org.apache.spark.sql.types.{IntegerType, StringType, StructField, StructType}
import org.apache.spark.{SparkConf, SparkContext}

object SpecifySchemaTest {
  def main(args: Array[String]): Unit = {

    val conf = new SparkConf().setAppName("sql-2").setMaster("local[1]")
    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc)
    val schema = StructType(List(StructField("id", IntegerType, true),
      StructField("name", StringType, true),
      StructField("age", IntegerType, true)
    )
    )
    val personRDD=sc.textFile("e://persons.txt").map(x => x.split(" "))

    val rowRDD=personRDD.map(x=>(Row(x(0).toInt,x(1).toString,x(2).toInt)))

    val personDataFrame=sqlContext.createDataFrame(rowRDD,schema);
    personDataFrame.registerTempTable("t_person1")
    val df=sqlContext.sql("select * from t_person1 order by age desc ")
    df.write.json("e://persons.json1")
    sc.stop();

  }
}
